/*
 * CustomerOperations.java
 *
 * Created on Jan 15, 2011, 2:52:55 PM
 */

package edu.rit.varapp.graphical;

import edu.rit.varapp.datalayer.CustomerOps;
import edu.rit.varapp.datalayer.Message;
import edu.rit.varapp.datalayer.Post;
import edu.rit.varapp.utilities.ExceptionUtils;
import java.awt.Dimension;
import java.sql.SQLException;
import javax.swing.JOptionPane;

/**
 * Allows a client to select what operations they would like the application to perform.
 * @author Eric Kisner
 */
public class CustomerOperations extends AbstractView {

    private final Post post = new Post();
    private final CustomerOps cOps = new CustomerOps();
    private int entityId;
    private static final int VIEW_WIDTH = 254;
    private static final int VIEW_HEIGHT = 324;


    /** Creates new form CustomerOperations */
    public CustomerOperations( int entityId ) {
        super (new Dimension(VIEW_WIDTH, VIEW_HEIGHT));
        initComponents();
        this.entityId = entityId;
        checkForNewMessages();
    }

    private void checkForNewMessages() {
        try {
            for( Message m : post.retrieveMessages( entityId ) ) {
                if( m.getStatus() == Message.UNREAD ) {
                    JOptionPane.showMessageDialog( rootPane, "You have new messages!" );
                    break;
                }
            }
        } catch( SQLException sqle ) {
            //We don't want the user to know we can't retrieve their messages.
            ExceptionUtils.handleException( sqle, this.getClass() );
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        infoeditButton = new javax.swing.JButton();
        purchasereviewButton = new javax.swing.JButton();
        viewAdminPostsButton = new javax.swing.JButton();
        logoutButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Client Operations");
        setMinimumSize(new java.awt.Dimension(254, 324));
        setResizable(false);
        getContentPane().setLayout(null);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel1.setText("Client Options");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(60, 20, 111, 22);

        jPanel1.setLayout(new java.awt.GridLayout(4, 1, 0, 15));

        infoeditButton.setText("Edit Your Information");
        infoeditButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                infoeditButtonActionPerformed(evt);
            }
        });
        jPanel1.add(infoeditButton);

        purchasereviewButton.setText("Review Your Purchases");
        purchasereviewButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                purchasereviewButtonActionPerformed(evt);
            }
        });
        jPanel1.add(purchasereviewButton);

        viewAdminPostsButton.setText("View Messages");
        viewAdminPostsButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                viewAdminPostsButtonActionPerformed(evt);
            }
        });
        jPanel1.add(viewAdminPostsButton);

        logoutButton.setText("Logout");
        logoutButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                logoutButtonActionPerformed(evt);
            }
        });
        jPanel1.add(logoutButton);

        getContentPane().add(jPanel1);
        jPanel1.setBounds(10, 60, 220, 220);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void logoutButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_logoutButtonActionPerformed
        new Login().setVisible( true );
        this.dispose();
    }//GEN-LAST:event_logoutButtonActionPerformed

    private void infoeditButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_infoeditButtonActionPerformed
        new CustomerEditInfo( entityId ).setVisible( true );
        this.dispose();
    }//GEN-LAST:event_infoeditButtonActionPerformed

    private void purchasereviewButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_purchasereviewButtonActionPerformed
        new CustomerDisplayPurchaseInfo( entityId ).setVisible( true );
        this.dispose();
    }//GEN-LAST:event_purchasereviewButtonActionPerformed

    private void viewAdminPostsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_viewAdminPostsButtonActionPerformed
        new ViewMessages( this, entityId).setVisible( true );
        this.setEnabled( false );
    }//GEN-LAST:event_viewAdminPostsButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton infoeditButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton logoutButton;
    private javax.swing.JButton purchasereviewButton;
    private javax.swing.JButton viewAdminPostsButton;
    // End of variables declaration//GEN-END:variables

}
